import requests

from comm.log_tools import log
from comm.yaml_tools import YamlTools
from conf.env import URL



class BaseRequest:
    #定义类变量 sess
    sess=requests.session()

    #1.定义发送请求的方法
    @classmethod
    def send_request(cls,**kwargs):
        try:
            rp=cls.sess.request(**kwargs)
        except Exception as e:
            log.error("接口请求失败")
        else:
            return rp
    @classmethod
    def execute_api_request(cls,request,parametrize):
        method =request["method"]
        cookies = request["cookies"]
        headers = request["headers"]
        url = URL+request["url"]
        data = parametrize["data"]
        params = parametrize["params"]
        json = parametrize["json"]
        files = parametrize["files"]
        expect = parametrize["expect"]
        rp=cls.send_request(method=method,
                         url=url,
                         headers=headers,
                         cookies=cookies,
                         data=data,
                         json=json,
                         params=params,
                         files=files
                         )
        return rp

if __name__ =='__main__':
    login_yaml_data=YamlTools.read_yaml_file('api_login_data.yaml')[0]
    rp=BaseRequest.execute_api_request(login_yaml_data["request"],login_yaml_data["parametrize"][0])
    print(rp.text)